package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityOrderReplaceData;
import com.ocom.common.entity.payconfig.EntityOrderReplaceItem;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.AddOrderReplaceByPersonRequest;
import com.ocom.common.request.payconfig.PauseOrderReplaceRequest;
import com.ocom.common.request.payconfig.RemoveOrderReplaceByPersonRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.PersonAndDeptVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoOrderReplaceData;
import com.ocom.payconfig.dao.IDaoPersonnelInfo;
import com.ocom.payconfig.service.OrderReplaceDataService;
import com.ocom.payconfig.service.OrderReplaceItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class OrderReplaceDataServiceImpl extends ServiceImpl<IDaoOrderReplaceData, EntityOrderReplaceData> implements OrderReplaceDataService {
    @Autowired
    private IDaoPersonnelInfo daoPersonnelInfo;

    @Autowired
    private OrderReplaceItemService orderReplaceItemService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Result addOrderReplaceByPerson(Long comId, AddOrderReplaceByPersonRequest request) {
        QueryWrapper<?> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(" p.com_id",request.getComId()).eq(" p.status",0).eq("p.p_code",request.getPCode()).eq("p.p_name",request.getPName()).orderByDesc("p.p_id").last("limit 1");

        PersonAndDeptVo personAndDept = daoPersonnelInfo.selectPerson(queryWrapper);
        if(ObjectUtil.isEmpty(personAndDept)){
            return ResultUtil.error(ResultEnum.Err_303);
        }

        //获取
        EntityOrderReplaceData entityOrderReplaceData = this.getOne(Wrappers.lambdaQuery(EntityOrderReplaceData.class).eq(EntityOrderReplaceData::getComId,request.getComId()).eq(EntityOrderReplaceData::getItemId,request.getItemId()).eq(EntityOrderReplaceData::getPId,personAndDept.getPId()).last("limit 1"));
        if(ObjectUtil.isEmpty(entityOrderReplaceData)){
            entityOrderReplaceData = new EntityOrderReplaceData();
            entityOrderReplaceData.setComId(request.getComId());
            entityOrderReplaceData.setDeptName(personAndDept.getDeptName());
            entityOrderReplaceData.setDeptId(personAndDept.getDeptId());
            entityOrderReplaceData.setDataStatus(1);
            entityOrderReplaceData.setPCode(personAndDept.getPCode());
            entityOrderReplaceData.setPName(personAndDept.getPName());
            entityOrderReplaceData.setItemId(request.getItemId());
            entityOrderReplaceData.setPId(personAndDept.getPId());
            this.save(entityOrderReplaceData);
        }
        return ResultUtil.success();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Result removeOrderReplaceByPerson(Long comId, RemoveOrderReplaceByPersonRequest request) {
        EntityOrderReplaceItem entityOrderReplaceItem = orderReplaceItemService.getById(comId,request.getItemId());
        if(ObjectUtil.isEmpty(entityOrderReplaceItem)){
            return ResultUtil.success();
        }
        List<String> pids = Arrays.asList(StrUtil.split(request.getPIds().trim(),","));
        if(ObjectUtil.isEmpty(pids)){
            return ResultUtil.success();
        }
        this.remove(Wrappers.lambdaQuery(EntityOrderReplaceData.class).eq(EntityOrderReplaceData::getItemId,request.getItemId()).eq(EntityOrderReplaceData::getDataStatus,1).eq(EntityOrderReplaceData::getPId,pids));
        return ResultUtil.success();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Result pauseOrderReplace(Long companyId, PauseOrderReplaceRequest request) {
        this.orderReplaceItemService.pauseOrderReplace(companyId,request);
        EntityOrderReplaceData entityOrderReplaceData = new EntityOrderReplaceData();
        entityOrderReplaceData.setDataStatus(3);

        this.update(entityOrderReplaceData,
                Wrappers.lambdaQuery(EntityOrderReplaceData.class)
                        .eq(EntityOrderReplaceData::getComId,companyId)
                        .eq(EntityOrderReplaceData::getItemId,request.getItemId())
                        .eq(EntityOrderReplaceData::getDataStatus,1));
        return ResultUtil.success();
    }
}
